package demo.demo07.test01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//实现Thread，实现多线程下载图片
public class testThreads2 extends Thread {
    private String url;//网络图片地址
    private String name;//保存文件名

    public testThreads2(String url, String name) {
        this.name = name;
        this.url = url;
    }

    //线程执行体
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载了文件名是：" + name);
    }

    public static void main(String[] args) {
        testThreads2 t1 = new testThreads2("http://img12.360buyimg.com/n1/jfs/t1/174190/2/11696/227597/60af0010Ed1db79ac/92077e107d908db7.jpg", "1.jpg");
        testThreads2 t2 = new testThreads2("http://img12.360buyimg.com/n1/jfs/t1/174190/2/11696/227597/60af0010Ed1db79ac/92077e107d908db7.jpg", "2.jpg");
        testThreads2 t3 = new testThreads2("http://img12.360buyimg.com/n1/jfs/t1/174190/2/11696/227597/60af0010Ed1db79ac/92077e107d908db7.jpg", "3.jpg");
//        testThreads2 t4 = new testThreads2("https://download.oracle.com/otn-pub/java/jdk/16.0.1+9/7147401fd7354114ac51ef3e1328291f/jdk-16.0.1_windows-x64_bin.zip?AuthParam=1622459155_943974d310e76f665fefa464ae3ef92f", "jdk.zip");

        t1.start();
        t2.start();
        t3.start();
//        t4.start();

    }
}


class WebDownloader {
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，下载方法出现问题");
        }
    }
}